package com.lf.web.service.system.extension;

import com.lf.data.ClusterUtil;
import com.lf.web.service.ResponseCode;
import com.lf.web.service.ResponseInfo;
import com.lf.web.service.ServiceSupport;
import org.apache.log4j.Logger;

import java.util.Date;

public class ExtensionAddService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(ExtensionAddService.class);
    private ExtensionDAO extensionDAO;

    public void setExtensionDAO(ExtensionDAO extensionDAO) {
        this.extensionDAO = extensionDAO;
    }

    public String execute() {
        String type = form.getString("type");
        if ("save".equals(type)) {
            return save();
        } else {
            return add();
        }
    }

    private String add() {
        context.put("check", ClusterUtil.getCheck(Extension.class));
        return ADD;
    }

    private String save() {
        Extension extension = new Extension();
        boolean result = form.getCluster(extension);
        LOGGER.debug("添加插件：" + extension);
        if (result) {
            String id = extension.setId();
            extension.put(Extension.CREATED, new Date());
            extensionDAO.insert(extension);
            ResponseInfo msg = new ResponseInfo(ResponseCode.FINISH);
            msg.put("id", id);
            out(msg);
        } else {
            error(404);
            LOGGER.error("非法添加插件：" + extension);
        }
        return NONE;
    }
}
